package ezy.lite;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;

import javax.microedition.khronos.opengles.GL10;

public class GPUInfo {
    public static interface OnInfoListener {
        void onInfo(String renderer, String vendor, String version);
    }

    public static void check(final Activity context, final OnInfoListener listener) {
        final WindowManager wm = context.getWindowManager();
        final GLSurfaceView view = new GLSurfaceView(context);
        view.setEGLConfigChooser(8, 8, 8, 8, 0, 0);
        view.setRenderer(new GLSurfaceView.Renderer() {
            @Override
            public void onDrawFrame(GL10 arg0) {
            }
            @Override
            public void onSurfaceChanged(GL10 arg0, int arg1, int arg2) {
            }

            @Override
            public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) {
                String renderer = gl.glGetString(GL10.GL_RENDERER);
                String vendor = gl.glGetString(GL10.GL_VENDOR);
                String version = gl.glGetString(GL10.GL_VERSION);
                Log.w("ezy", "------------------ GL_INFO ------------------");
                Log.w("ezy", "GL_RENDERER = " + renderer);
                Log.w("ezy", "GL_VENDOR   = " + vendor);
                Log.w("ezy", "GL_VERSION  = " + version);
                Log.w("ezy", "---------------------------------------------");
                if (listener != null) {
                    listener.onInfo(renderer, vendor, version);
                }
                view.post(new Runnable() {
                    @Override
                    public void run() {
                        wm.removeView(view);
                    }
                });
            }
        });

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        lp.format = PixelFormat.RGBA_8888;
        lp.width = 1;
        lp.height = 1;
        lp.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

        wm.addView(view, lp);
    }
}